package me.shurufa.activities;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.event.EventObject;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 在发现页点击新建跳转的页面
 * @author andy
 */
public class NewDigestBookActivity extends BaseActivity implements View.OnClickListener {

    public static final int SCAN_REQUEST_CODE = 3927;

    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.toolbar_divider)
    View toolbarDivider;

    @Bind(R.id.search_toolbar)
    LinearLayout search_toolbar;

    // 扫描
    @Bind(R.id.iv_scan)
    ImageView iv_scan;

    private int mFromWhere;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_new_digest_book;
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        parseArguments();
        uiInit();
    }

    public static void initArgument(Intent in, int fromWhere) {
        in.putExtra(Constants.ARG_FROM_WHERE, fromWhere);
    }

    private void parseArguments() {
        mFromWhere = getIntent().getIntExtra(Constants.ARG_FROM_WHERE, 0);
    }

    private void uiInit() {
        leftText.setText(getString(R.string.cancel));
        backInToolbar.setVisibility(View.GONE);
        leftText.setVisibility(View.VISIBLE);
        titleTextView.setText(getString(R.string.new_book));
        toolbarDivider.setVisibility(View.GONE);
        leftText.setOnClickListener(this);

        iv_scan.setOnClickListener(this);
        search_toolbar.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.left_text:
                finish();
                break;
            case R.id.iv_scan:
                checkCameraPermission();
                break;
            case R.id.search_toolbar:
                intent = new Intent(NewDigestBookActivity.this, NewDigestBookSearchActivity.class);
                NewDigestBookSearchActivity.initArguments(intent, Constants.SEARCH_BY_KEYWORD, mFromWhere);
                startActivity(intent);
                break;
        }
    }

    /**
     * 检测runtime权限
     */
    private void checkCameraPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            int checkCameraPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
            if (checkCameraPermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, Constants.REQUEST_CAMERA);
                return;
            } else {
                startScanAct();
            }
        } else {
            startScanAct();
        }
    }

    /**
     * 扫描界面
     */
    private void startScanAct() {
        Intent intent = new Intent(NewDigestBookActivity.this, QRScanActivity.class);
        startActivityForResult(intent, SCAN_REQUEST_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case Constants.REQUEST_CAMERA:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startScanAct();
                } else {
                    Utils.showToast(R.string.permission_decline);
                }
                break;
            case Constants.REQUEST_WRITE:
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    Utils.showToast(R.string.permission_decline);
                    finish();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SCAN_REQUEST_CODE) {
            if (resultCode == QRScanActivity.RESULT_CODE) {
                String isbn = data.getStringExtra(Constants.EXTRA_SCAN_RESULT);
                if (!TextUtils.isEmpty(isbn)) {
                    Intent intent = new Intent(NewDigestBookActivity.this, NewDigestBookSearchActivity.class);
                    NewDigestBookSearchActivity.initArguments(intent, Constants.SEARCH_BY_ISBN, isbn, mFromWhere);
                    startActivity(intent);
                }
            }
        }
    }


    public void onEventMainThread(EventObject object) {
    }

    @Override
    public void onEventMainThread(Intent intent) {
        super.onEventMainThread(intent);
        if (intent.getAction().equals(Constants.EVENT_ADD_NOTEBOOK_FROM_ADD_DIGEST)) {
            finish();
        }
    }

}
